package com.csis.framework.util.jqgrid;

import com.csis.framework.domain.FormBean;
import com.csis.framework.util.jqgrid.JqGridResponse.ResponseType;

public class JqGridUtils {

	/**
	 * get {@link JqGridResponseParser}
	 * 
	 * @param <E>
	 *            entity class type
	 * @param <T>
	 *            which type extends {@link FormBean}
	 * @param formBeanType
	 *            which class extends {@link FormBean}
	 * @param responseType
	 *            {@link ResponseType}
	 * @param filter
	 *            {@link JqGridResponseFilter}
	 * @return {@link JqGridResponseParser}
	 */
	public static <E, T extends FormBean<E>> JqGridResponseParser<T> getParser(Class<T> formBeanType,
			ResponseType responseType, JqGridResponseFilter<E> filter) {
		AbstractFormBeanResponseParser<E, T> abstractFormBeanResponseParser = null;
		switch (responseType) {
		case JSON:
			abstractFormBeanResponseParser = new JqGridJSONResponseParser<E, T>();
			break;
		default:
			abstractFormBeanResponseParser = new JqGridXMLResponseParser<E, T>();
			break;
		}
		if (filter != null)
			abstractFormBeanResponseParser.setFilter(filter);
		return abstractFormBeanResponseParser;
	}

	/**
	 * get {@link JqGridResponseParser}, its {@link JqGridResponseFilter} is
	 * {@link DefaultJqGridResponseFilter}
	 * 
	 * @param <E>
	 * @param <T>
	 * @param formBeanType
	 * @param responseType
	 * @return
	 */
	public static <E, T extends FormBean<E>> JqGridResponseParser<T> getParser(Class<T> formBeanType,
			ResponseType responseType) {
		return getParser(formBeanType, responseType, null);
	}

	/**
	 * get {@link JqGridResponseParser}, its {@link JqGridResponseFilter} is
	 * {@link DefaultJqGridResponseFilter}; and its {@link ResponseType} is
	 * {@link ResponseType.JSON}
	 * 
	 * @param <E>
	 * @param <T>
	 * @param formBeanType
	 * @param responseType
	 * @return
	 */
	public static <E, T extends FormBean<E>> JqGridResponseParser<T> getJSONParser(Class<T> formBeanType) {
		return getParser(formBeanType, ResponseType.JSON, null);
	}

	/**
	 * get {@link JqGridResponseParser}, its {@link JqGridResponseFilter} is
	 * {@link DefaultJqGridResponseFilter}; and its {@link ResponseType} is
	 * {@link ResponseType.XML}
	 * 
	 * @param <E>
	 * @param <T>
	 * @param formBeanType
	 * @param responseType
	 * @return
	 */
	public static <E, T extends FormBean<E>> JqGridResponseParser<T> getXMLParser(Class<T> formBeanType) {
		return getParser(formBeanType, ResponseType.XML, null);
	}
}
